<template>
  <el-container class="homeBox">
    <el-header class="home-header">
      <el-row>
        <el-col :span="8">
          <img src="../../assets/images/logo.png" alt>
        </el-col>
        <el-col :span="8" class="title">
          <h1>电商后台管理系统</h1>
        </el-col>
        <el-col :span="8" class="outLogin">
          欢迎上海前端100期星曜会员
          <a to="#" @click.prevent="loginOut">退出</a>
        </el-col>
      </el-row>
    </el-header>
    <el-container class="mainBox">
      <el-aside width="200px" class="aside">
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :router="true"
          ref="menu"
        >
          <el-submenu :index="item.id+''" v-for="item in menus" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+children.path"
              v-for="children in item.children"
              :key="children.id"
            >{{children.authName}}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: []
    }
  },
  methods: {
    loginOut () {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
          sessionStorage.removeItem('token')
          this.$router.push('/login')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    }
  },
  async created () {
    const res = await this.axios.get(
      'menus'
    )
    this.menus = res.data.data
  }
}
</script>

<style lang="less">
.home-header {
  padding: 0;
  height: 60px;
  background-color: #b3c1cd;
}
.title {
  text-align: center;
  line-height: 60px;
  h1 {
    color: #fff;
    font-size: 28px;
  }
}
.outLogin {
  text-align: right;
  padding-right: 40px;
  line-height: 60px;
  a {
    color: #daa520;
  }
}
.homeBox {
  min-width: 890px;
  height: 100%;
  .mainBox {
    height: 100%;
    .aside {
      background-color: #545c64;
      height: 100%;
    }
  }
}
</style>
